package cn.gov.pbc.tims.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gov.pbc.tims.entity.Type;
import cn.gov.pbc.tims.entity.Typegroup;

@Service("systemService")
@Transactional
public class DictService extends CommonService  {
 
	/**
	 * 根据类型编码和类型名称获取Type,如果为空则创建一个
	 * 
	 * @param typecode
	 * @param typename
	 * @return
	 */
	public Type getType(String typecode, String typename, Integer hbTypegroupId) {
		Type actType = (Type) commonDao.findUniqueByProperty(Type.class, "typecode", typecode);
		if (actType == null) {
			actType = new Type();
			actType.setTypecode(typecode);
			actType.setTypename(typename);
			actType.setHbTypegroupId(hbTypegroupId);
			commonDao.save(actType);
		}
		return actType;

	}
	/**
	 * 根据group Code获取Types 
	 * 
	 * @param typegroupcode
	 * @return
	 */
	public List<Type> getTypesByGroupCode(String typegroupcode) {
		Typegroup HbTypegroup=(Typegroup)commonDao.findUniqueByProperty(Typegroup.class, "typegroupcode", typegroupcode);
		System.out.println(HbTypegroup.getId());
		List<Type> types = (List<Type>) commonDao.findByProperty(Type.class, "hbTypegroupId", HbTypegroup.getId());
		return  types;
		}
	/**
	 * 根据group Id获取Types 
	 * 
	 * @param typegroupId
	 * @return
	 */
	public List<Type> getTypesByGroupId(Integer typegroupId) {
		Typegroup HbTypegroup=(Typegroup)commonDao.get(Typegroup.class,typegroupId);
		List<Type> types = (List<Type>) commonDao.findByProperty(Type.class, "hbTypegroupId",typegroupId);
		return  types;
		}
			
 
	/**
	 * 根据类型分组编码和名称获取TypeGroup,如果为空则创建一个
	 * 
	 * @param typecode
	 * @param typename
	 * @return
	 */
	public Typegroup getTypeGroup(String typegroupcode, String typgroupename) {
		Typegroup HbTypegroup = (Typegroup) commonDao.findUniqueByProperty(Typegroup.class, "typegroupcode", typegroupcode);
		if (HbTypegroup == null) {
			HbTypegroup = new Typegroup();
			HbTypegroup.setTypegroupcode(typegroupcode);
			HbTypegroup.setTypegroupname(typgroupename);
			commonDao.save(HbTypegroup);
		}
		return HbTypegroup;
	}

	
	public Typegroup getTypeGroupByCode(String typegroupCode) {
		Typegroup HbTypegroup = (Typegroup) commonDao.findUniqueByProperty(Typegroup.class, "typegroupcode", typegroupCode);
		return HbTypegroup;
	}

	
//	public void initAllTypeGroups() {
//		List<HbTypegroup> typeGroups = this.commonDao.loadAll(HbTypegroup.class);
//		for (HbTypegroup HbTypegroup : typeGroups) {
//			HbTypegroup.allTypeGroups.put(HbTypegroup.getTypegroupcode().toLowerCase(), HbTypegroup);
//			List<HbType> types = this.commonDao.findByProperty(HbType.class, "HbTypegroup.id", HbTypegroup.getId());
//			HbTypegroup.allTypes.put(HbTypegroup.getTypegroupcode().toLowerCase(), types);
//		}
//	}

	
	/*public void refleshTypesCach(HbType type) {
		HbTypegroup HbTypegroup = type.getHbTypegroup();
		HbTypegroup typeGroupEntity = (HbTypegroup) this.commonDao.get(HbTypegroup.class, HbTypegroup.getId());
		List<HbType> types = this.commonDao.findByProperty(HbType.class, "HbTypegroup.id", HbTypegroup.getId());
		HbTypegroup.allTypes.put(typeGroupEntity.getTypegroupcode().toLowerCase(), types);
	}*/

	
//	public void refleshTypeGroupCach() {
//		HbTypegroup.allTypeGroups.clear();
//		List<HbTypegroup> typeGroups = this.commonDao.loadAll(HbTypegroup.class);
//		for (HbTypegroup HbTypegroup : typeGroups) {
//			HbTypegroup.allTypeGroups.put(HbTypegroup.getTypegroupcode().toLowerCase(), HbTypegroup);
//		}
//	}
// 
 


}
